home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 593 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets() question
  5. Date: 7 Jan 1996 09:44 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <7JAN199609444189@erich.triumf.ca>
  9. References: <4cosgf$rir@newsbf02.news.aol.com>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4cosgf$rir@newsbf02.news.aol.com>, simpsondg@aol.com (SimpsonDG) writes...
  14. >Can any of you C gurus out there help me with this?  I have a problem with
  15. >gets() that I don't understand.  The program below will successfully input
  16. >the string s[0], but will simply ignore the gets() that inputs s[1] and
  17. >goes on to ask for i[1].  What's the deal?  A little experimenting seems
  18. >to indicate that the problem arises when I have a gets() following a
  19. >scanf() -- e.g., a program using only gets() or only scanf() works fine.
  20.  
  21. >#include "stdio.h"
  22. >void main(void)
  23.  
  24. To avoid flames in comp.lang.c (and because the C standard says so), this _must
  25. be:
  26.     int main(void)
  27. in spite of many books to the contrary.
  28. (then you need "return 0;" at the end of main() )
  29.  
  30. Unless you are careful with the first argument to scanf(), it will leave a
  31. newline in the input buffer.  If you use only scanf()s, this is no problem, as
  32. the next scanf() will consider the newline as "white space" and just read past
  33. it.  However, most other input functions will just read up to, and including, a
  34. newline, so seem to be ignored following a scanf().
  35.  
  36. A better input method is to _only_ use fgets() (_never_ gets()!), then use
  37. sscanf(), strtok(), atoi(), or whatever to parse the input - this avoids the
  38. problem you are having, and allows you to do some defensive coding, to catch
  39. invalid input. If the user enters something shorter than the buffer size
  40. specified for fgets(), fgets() will leave a '\n' at the end, which you may need
  41. to remove...
  42.  
  43. gets() should not be used because it allows the user to enter an infinitely
  44. long string, and write far beyond the space you allocated for input, which will
  45. corrupt other variables or code.
  46.  
  47.  
  48. >{
  49. >   int i[5];
  50. >   char s[3][10];
  51. >   printf ("Enter s[0]:  ");
  52. >   gets (s[0]);
  53. >   printf ("Enter i[0]]:  ");
  54. >   scanf ("%d",&i[0]);
  55. >   printf ("Enter s[1]:  ");
  56. >   gets (s[1]);                  /* this gets() doesn't wait for input */
  57. >   printf ("Enter i[1]]:  ");
  58. >   scanf ("%d",&i[1]);
  59. >}
  60. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  61. Internet: bennett@triumf.ca         | of one another only when one can be
  62. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  63. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  64. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  65.  
  66.